//
//  SKStatusToolBar.swift
//  WeiboProjectYL
//
//  Created by yanli on 16/7/13.
//  Copyright © 2016年 mountainPassCell. All rights reserved.
//

import UIKit

//  微博toolbar
class SKStatusToolBar: UIView {

    // MARK: -- 懒加载控件
    // 初始化并添加控件
    // 转发数 Btn
    private lazy var retweetButton: UIButton = self.addChildButton("timeline_icon_retweet", title: "转发")
    // 评论 Btn
    private lazy var commentButton: UIButton = self.addChildButton("timeline_icon_comment", title: "评论")
    // 赞 Btn
    private lazy var unlikeButton: UIButton = self.addChildButton("timeline_icon_unlike", title: "赞")
    
    
    //数据模型
    var statusViewModel: SKStatusViewModel? {
        didSet {
            
            retweetButton.setTitle(statusViewModel?.retweetStr, forState: .Normal)
            commentButton.setTitle(statusViewModel?.commentStr, forState: .Normal)
            unlikeButton.setTitle(statusViewModel?.unlikeStr, forState: .Normal)
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        //  添加控件设置约束
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    //  添加控件设置约束  (在创建按钮的共用方法中,已经将btn添加到视图)
    private func setupUI() {
        
        // 设置按钮相应的约束
        // 转发 Btn
        retweetButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        // 评论 Btn
        commentButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetButton.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton)
        }
        // 赞 👍
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
        }
        
        
        // 添加线
        let firstLine = addChildLine()
        let secondLine = addChildLine()
        // 设置图片的约束
        firstLine.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton.snp_trailing)
            make.centerY.equalTo(self.snp_centerY)
        }
        secondLine.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(self.snp_centerY)
        }
        
    }
    
    
    
    // 按钮中间的竖线
    private func addChildLine() -> UIImageView {
        
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        // 添加控件
        addSubview(imageView)
        
        return imageView
    }
    
    
    // MARK: -- 创建按钮的共用方法
    
    private func addChildButton(imageName: String, title: String) -> UIButton {
        
        // 创建 UIButton
        let button = UIButton()
        // 设置图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        
        // 取消高亮
        button.adjustsImageWhenHighlighted = false
        //  设置背景色
        //button.backgroundColor = UIColor(patternImage: UIImage(named: "timeline_card_bottom_background")!)
        
        // 设置文字
        button.setTitle(title, forState: .Normal)
        // 指定文字大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        // 指定文字颜色
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        
        //  添加按钮
        addSubview(button)
        
        return button
    }
    
    
    
    
    
    
    
}


















